package com.frame.base.utils;

import android.content.Context;
import android.text.Html;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/10/24.
 */

public class StringUtil {
    /**
     * 判断是否为空：（null:不区分大小写； ""）
     *
     * @return true:空，false:不为空
     */
    public static boolean isEmpty(String str) {
        return str == null || str.trim().equals("") || str.trim().equalsIgnoreCase("null");
    }
    /**
     * 获取后缀自定义颜色的字符串
     *
     * @param text
     * @param status
     * @param statusColor
     * @return
     */
    public static CharSequence getStringWithStatus(String text, String status, String statusColor) {
        StringBuffer sb = new StringBuffer(text);
        sb.append(" <font color=\"" + statusColor + "\">" + status + "</font>");
        return Html.fromHtml(sb.toString());
    }

    /**
     * 获取后缀自定义颜色的字符串
     *
     * @param text
     * @param status
     * @param statusColor
     * @return
     */
    public static CharSequence getStringWithSmallStatus(String text, String status, String statusColor) {
        if (text==null) return "";
        StringBuffer sb = new StringBuffer(text);
        sb.append(" <font color=\"" + statusColor + "\"><small>" + status + "</small></font>");
        return Html.fromHtml(sb.toString());
    }
    /**
     * 将String 转换成Int 如果空或者异常则返回0
     */
    public static int parseInt(String str) {
        return parseInt(str, 0);
    }

    /**
     * 将String 转换成Int 如果空或者异常则返回0
     *
     * @param defValue 转换异常返回的数据
     */
    public static int parseInt(String str, int defValue) {
        if (isEmpty(str))
            return defValue;
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return defValue;
        }
    }

    public static boolean checkEmail(String email) {
        String str = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public static boolean checkMobileNO(String value) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(14[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(value);
        return m.matches();
    }


    public static boolean checkPassword(String value) {
        Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkUri(String value) {
        Pattern p = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkFullName(String value) {
        Pattern p = Pattern.compile("^[\u4e00-\u9fa5]{2,4}");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkClassName(String value) {
        Pattern p = Pattern.compile("^[a-zA-Z0-9\u4e00-\u9fa5]{1,}");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkNickName(String value) {
        Pattern p = Pattern.compile("^[a-zA-Z0-9\u4e00-\u9fa5]{2,7}");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    public static boolean checkUserName(String value) {
        Pattern p = Pattern.compile("^(?![0-9]+$)[0-9A-Za-z]{6,16}$");
        Matcher m = p.matcher(value);
        return m.matches();
    }

    /**
     * 生成随机字符串
     *
     * @param length 字符串长度
     * @return
     */
    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static boolean showEmptToast(Context context, TextView editText, String tip) {
        String s = editText.getText().toString();
        if (StringUtil.isEmpty(s)) {
            Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }

    }

    public static String phoneHideMiddle(String phone) {
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    public static String EmalHideMiddle(String phone) {
        return phone.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
    }
}
